package com.star.links.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.star.links.Service.PlayService;

public class MyView extends View {

    private  PlayService playService;

    private Paint paint;

    //选中的方块
    private  Block selectedBlock;
    //选中的图片
    private Bitmap selectedImage;

    public MyView(Context context, AttributeSet attrs){
        super(context,attrs);
        this.paint = new Paint();
        // 设置连接线的颜色
        this.paint.setColor(Color.RED);
        // 设置连接线的粗细
        this.paint.setStrokeWidth(3);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.playService == null)
            return;
        Block[][] blocks = playService.getBlocks();
        if (blocks != null) {
            // 遍历pieces二维数组
            for (int i = 0; i < blocks.length; i++) {
                for (int j = 0; j < blocks[i].length; j++) {
                    // 如果二维数组中该元素不为空（即有方块），将这个方块的图片画出来
                    if (blocks[i][j] != null) {
                        // 得到这个Piece对象
                        Block block = blocks[i][j];
                        // 根据方块左上角X、Y座标绘制方块
                        canvas.drawBitmap(block.getImage().getImage(),
                                block.getLocationX(), block.getLocationY(), null);
                    }
                }
            }
        }
    }

    public void setPlayService(PlayService playService) {
        this.playService = playService;
    }

    public void setSelectedBlock(Block selectedBlock) {
        this.selectedBlock = selectedBlock;
    }
    // 开始游戏方法
    public void startGame()
    {
        this.playService.onStart();
        //重新绘制view
        this.postInvalidate();
    }
}
